﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ZeroBug.EasyFlow.Core;

namespace ZeroBug.EasyFlow.Step
{
    public class ShowWarnStep : IStep
    {
        public bool IsSingleRun { get; set; }
        public bool IgnoreChildren { get; set; }
        public string Icon { get; set; } = "Info";
        public int ChildrenIndex { get; set; }
        public Core.Step? NextStep { get; set; }

        VariableExpression alarmInfo;
        VariableExpression tryAgainStepName;
        VariableExpression skipToStepName;
        public bool Compile(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;

            if (!step.Parameters.ContainsKey("AlarmInfo"))
            {
                errInfo = "不存在AlarmInfo变量";
                return false;
            }
            alarmInfo = step.Parameters["AlarmInfo"];

            if (!step.Parameters.ContainsKey("TryAgainStepName"))
            {
                errInfo = "不存在TryAgainStepName变量";
                return false;
            }
            tryAgainStepName = step.Parameters["TryAgainStepName"];

            if (step.Parameters.ContainsKey("SkipToStepName"))
            {
                skipToStepName = step.Parameters["SkipToStepName"];
            }

            return true;
        }

        public object GetForm()
        {
            throw new NotImplementedException();
        }

        public StepInfo GetInfo()
        {
            StepInfo info = new StepInfo();
            info.StepName = "ShowWarn";
            info.Group = StepGroup.UI;
            info.ImagePath = "pack://application:,,,/ZeroBug.EasyFlow.Step;component/Images/条件.png";
            return info;
        }

        public void OnChildrenEnter()
        {

        }

        public void OnChildrenLeave()
        {

        }

        public bool Prepare(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;

            return true;
        }

        public void RegisterVariables(FlowBase flow, Core.Step step)
        {

        }

        public StepResult Run(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;

            NextStep = null;

            MessageBoxResult mbr = MessageBox.Show($"{alarmInfo.GetEquivalent()[0]}", "报警", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning);

            //如果是重试
            if (mbr == MessageBoxResult.Yes)
            {
                NextStep = flow.GetStep(tryAgainStepName.GetEquivalent()[0]);
            }
            //如果是继续
            else if (mbr == MessageBoxResult.No)
            {
                NextStep = null;
            }
            //如果是跳转
            else
            {
                if(skipToStepName != null)
                    NextStep = flow.GetStep(skipToStepName.GetEquivalent()[0]);
            }

            return StepResult.Success;
        }

        public string GetTip(Core.Step step)
        {
            return "";
        }

        public (SerializableDictionary<string, VariableExpression>, List<VariableJudge>) GetParametersAndConditions()
        {
            throw new NotImplementedException();
        }

        public void UnRegisterVariables(FlowBase flow, Core.Step step)
        {
        }
    }
}
